import {ContentChild, Directive, Input} from '@angular/core';
import {EndlessListDataDirective} from "../endless-list-data/endless-list-data";

@Directive({
    selector: '[endless-list]' // Attribute selector
})
export class EndlessListDirective {

    @ContentChild(EndlessListDataDirective) _itmTmp: EndlessListDataDirective;

    constructor() {
    }

    @Input('endless-list') set endlessDist(data){
        this.populateNodeData(data);
    }

    populateNodeData(data:any[]){
        if(data && data.length){
            data.forEach((item,i) => {
                this._itmTmp.viewContainer.createEmbeddedView(this._itmTmp.templateRef,{
                    $implicit:item,
                    index:i,
                    count:data.length
                },i);
            })
        }else{
            this._itmTmp.viewContainer.clear();
        }
    }

}
